package learn.advance.generic1;

import com.sun.tools.javah.Gen;
import org.omg.CORBA.INTERNAL;
import sun.net.www.content.text.Generic;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;

//import com.czht.wdp.protobuf.FacedataProcess;
//import com.czht.wdp.protobuf.FacedataProcess.Message;
//

class GenericClass<E> {
    private E name_;

    public E GetName() {
        return name_;
    }

    public void SetName(E name) {
        this.name_ = name;
    }
}


class Demo02GenericClass {
    public static void Test() {
        //默认Object类型
        GenericClass gc = new GenericClass();

        gc.SetName("字符串");
        Object obj = gc.GetName();
        System.out.println("obj=" + obj);

        Object o = new Object();
        System.out.println("o=" + o);

        GenericClass<Integer> gc2 = new GenericClass<>();
        gc2.SetName(11);
        Integer name = gc2.GetName();
        System.out.println("name=" + name);
    }
}

class GenericMethod {
    public <M> void Method1(M m) {
        System.out.println(m);
    }

    public static <S> void Method2(S s) {
        System.out.println(s);
    }
}

class Demo03GenericMethod {
    public static void Test() {
        GenericMethod gm = new GenericMethod();
        gm.Method1(10);
        gm.Method2("abc");

        GenericMethod.Method2("静态方法");
    }
}

interface GenericInterfacr<I> {
    public abstract void Method(I i);
}

class GenericInterfaceImpl1 implements GenericInterfacr<String> {

    @Override
    public void Method(String s) {
        System.out.println(s);
    }
}

class GenericInterfaceImpl2<I> implements GenericInterfacr<I> {

    @Override
    public void Method(I i) {
        System.out.println("实现类：" + i);
    }
}

class Demo4GenericInterface {
    public static void Test() {
        GenericInterfaceImpl1 g1 = new GenericInterfaceImpl1();
        g1.Method("字符串");

        GenericInterfaceImpl2<Integer> g2 = new GenericInterfaceImpl2<>();
        g2.Method(10);
    }
}

class Demo5Generic {
    public static void Test() {
        ArrayList<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);

        ArrayList<String> list2 = new ArrayList<>();
        list2.add("a");
        list2.add("abc");

        PrintArray(list1);
        PrintArray(list2);
    }

    public static void PrintArray(ArrayList<?> list) {

        Iterator<?> it = list.iterator();

        while (it.hasNext()) {
            Object o = it.next();
            System.out.println(o);
        }
    }
}

class DemoGeneric {

    public static void GetElement1(Collection<? extends Number> c) {

    }

    public static void GetElement2(Collection<? super Number> c) {

    }

    public static void Test() {
        Collection<Integer> list1 = new ArrayList<>();
        Collection<String> list2 = new ArrayList<>();

        Collection<Number> list3 = new ArrayList<>();
        Collection<Object> list4 = new ArrayList<>();

        GetElement1(list1);
        //GetElement2(list2); 错误
        GetElement1(list3);
        //GetElement1(list4); 错误

        //GetElement2(list1); 错误
        //GetElement2(list2); 错误、
        GetElement2(list3);
        GetElement2(list4);
    }
}


public class TestGeneric {

    public static void main(String[] args) {
        //Demo02GenericClass.Test();
        //Demo03GenericMethod.Test();
        //Demo4GenericInterface.Test();
        //Demo5Generic.Test();

    }

}
